package com.et.drive.info

import com.et.logger.ELog
import com.et.logger.TAGS
import com.et.machine.convert.IMachineSignInfo
import com.et.machine.convert.machineInfo
import com.et.util.headInsert2String
import com.et.util.toHexString

data class MachineInfo(
    var version: String,
    var uuid: String,
    var type: String,
    var cDrink: Int,
    var cFood: Int,
    var cBox: Int,
    var nDrink: Int,
    var nFood: Int,
    var nBox: Int
) {
    override fun toString(): String {
        return "MachineInfo(" +
                " version='$version'," +
                " uuid='$uuid'," +
                " type='$type'," +
                " cDrink=$cDrink," +
                " cFood=$cFood," +
                " cBox=$cBox," +
                " nDrink=$nDrink," +
                " nFood=$nFood," +
                " nBox=$nBox )"
    }
}


object IMachineSignInfoImpl : IMachineSignInfo {
    private var list: HashSet<((MachineInfo) -> Unit)> = HashSet()
    override fun observer(_callback: (MachineInfo) -> Unit) {
        list.add(_callback)
    }

    fun machineInfoNotify(byteArray: ByteArray) {
        MachineInfo(
            version = byteArray.toHexString(0, 2),
            uuid = byteArray.toHexString(2, 4),
            type = byteArray[13].toString(16).headInsert2String('0', 2),
            cDrink = byteArray[14].toInt(),
            cFood = byteArray[15].toInt(),
            cBox = byteArray[16].toInt(),
            nDrink = byteArray[17].toInt(),
            nFood = byteArray[18].toInt(),
            nBox = byteArray[19].toInt(),
        ).also { info ->
            machineInfo = info
            ELog.d(TAGS.machine, "Sign Info $info")
            if (list.isNotEmpty()) {
                list.forEach {
                    it(info)
                }
            }
        }
    }
}